// export const FUNERAL_PARLOUR = [
//     {
//         cemetery: '泰州市靖江殡仪馆',
//         // img: require('@/assets/funeralParlour/funeralParlour.png'),
//         img: new URL('@/assets/funeralParlour/funeralParlour.png', import.meta.url).href,
//         addr: '江苏省泰州市靖江市',
//         name: '',
//         phone: ''
//     },
// ]
export const NATIONS = ['汉族', '壮族', '满族', '回族', '苗族', '维吾尔族', '彝族', '土家族', '蒙古族', '藏族', '布依族', '侗族', '瑶族', '朝鲜族', '白族',
'哈尼族', '哈萨克族', '黎族', '傣族', '畲族', '傈僳族', '仡佬族', '拉祜族', '高山族', '东乡族', '佤族', '水族', '纳西族', '羌族', '土族',
'锡伯族', '仫佬族', '柯尔克孜族', '达斡尔族', '景颇族', '傈僳族', '撒拉族', '布朗族', '毛难族', '塔吉克族', '普米族', '阿昌族', '怒族',
'鄂温克族', '京族', '基诺族', '德昂族', '乌孜别克族', '俄罗斯族', '保安族', '裕固族', '门巴族', '鄂伦春族', '独龙族', '塔塔尔族', '赫哲族',
'珞巴族', '其他', '外国血统中国籍人士'
]
// 检测身份证是否非法
export function isCardID(sId) {
    var aCity = {
        11: "北京",
        12: "天津",
        13: "河北",
        14: "山西",
        15: "内蒙古",
        21: "辽宁",
        22: "吉林",
        23: "黑龙江",
        31: "上海",
        32: "江苏",
        33: "浙江",
        34: "安徽",
        35: "福建",
        36: "江西",
        37: "山东",
        41: "河南",
        42: "湖北",
        43: "湖南",
        44: "广东",
        45: "广西",
        46: "海南",
        50: "重庆",
        51: "四川",
        52: "贵州",
        53: "云南",
        54: "西藏",
        61: "陕西",
        62: "甘肃",
        63: "青海",
        64: "宁夏",
        65: "新疆",
        71: "台湾",
        81: "香港",
        82: "澳门",
        91: "国外"
    };
    var iSum = 0;
    var info = "";
    if (!/^\d{17}(\d|x)$/i.test(sId)) return "身份证长度或格式错误";
    sId = sId.replace(/x$/i, "a");
    if (aCity[parseInt(sId.substr(0, 2))] == null) return "身份证地区非法";
    let sBirthday = sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2));
    var d = new Date(sBirthday.replace(/-/g, "/"));
    if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) return "身份证上的出生日期非法";
    for (var i = 17; i >= 0; i--) iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);
    if (iSum % 11 != 1) return "身份证号非法";
    //aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女");//此次还可以判断出输入的身份证号的人性别
    return "";
}
// 获取性别
export function getSex(idCard) {
    //alert(idCard);
    var sexStr = '';
    if (parseInt(idCard.slice(-2, -1)) % 2 == 1) {
        sexStr = '男';
    } else {
        sexStr = '女';
    }
    return sexStr;
}
// 获取年龄
export function getAge(identityCard) {
    var len = (identityCard + "").length;
    if (len == 0) {
        return '';
    } else {
        if ((len != 15) && (len != 18))//身份证号码只能为15位或18位其它不合法
        {
            return '';
        }
    }
    var strBirthday = "";
    if (len == 18)//处理18位的身份证号码从号码中得到生日和性别代码
    {
        strBirthday = identityCard.substr(6, 4) + "/" + identityCard.substr(10, 2) + "/" + identityCard.substr(12, 2);
    }
    if (len == 15) {
        strBirthday = "19" + identityCard.substr(6, 2) + "/" + identityCard.substr(8, 2) + "/" + identityCard.substr(10, 2);
    }
    //时间字符串里，必须是“/”
    var birthDate = new Date(strBirthday);
    var nowDateTime = new Date();
    var age = nowDateTime.getFullYear() - birthDate.getFullYear();
    //再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
    if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
        age--;
    }
    return age;
}
// 日期格式化
export function dateformatter(date) {
    let YY = date.getFullYear() + '-';
    let MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    let DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    return YY + MM + DD;
}
// 获取日期-时间
export const getDateTime = (dateTime = new Date()) => {
    return dateTime.getFullYear() + '-' + (dateTime.getMonth() + 1).toString().padStart(2, 0) + '-' + dateTime.getDate().toString().padStart(2, 0) + ' ' + dateTime.getHours().toString().padStart(2, 0) + ':' + dateTime.getMinutes().toString().padStart(2, 0) + ':' + dateTime.getSeconds().toString().padStart(2, 0)
}
// 手机号
export function isPhone(val) {
    let reg = /^[1][0,1,2,3,4,5,6,7,8,9][0-9]{9}$/;
    return reg.test(val)
}
export function getDaysDiff(date1, date2) {
  // 获取两个日期对象的毫秒数
  let time1 = date1.getTime();
  let time2 = date2.getTime();
  // 计算相差的毫秒数
  let diff = Math.abs(time1 - time2);
  // 将毫秒数转换为天数
  let days = Math.floor(diff / (1000 * 60 * 60 * 24));
  return days;
}
// 腾讯地图坐标转百度
export function qqMapTransBMap(longitude, latitude) {
      let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
      let x = longitude;
      let y = latitude;
      let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
      let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
      let longitudes = z * Math.cos(theta) + 0.0065;
      let latitudes = z * Math.sin(theta) + 0.006;
     
      return {
          longitude: longitudes,
          latitude: latitudes 
      } 
}
// 压缩图片
export class compressImg{//定义了一个名字为compressImg的类
    // constructor(file,num,fun){//constructor是一个构造方法，用来接收参数
    //     this.file=file;
    //     // this.num=num;
    //     // this.fun = fun
    //     // this.imgPreview(this.file,this.num)
    //     this.imgPreview(this.file)
    //     this.myf = ''
    //     this.myfb = ''
    // }
    constructor(file, fun,...args){//constructor是一个构造方法，用来接收参数
        this.file=file;
        this.args=args;
        this.fun = fun
        this.imgPreview(this.file)
        this.myf = ''
        this.myfb = ''
        console.log(args)
        console.log(this.fun)
    }
    // 处理图片
    imgPreview(file,num) {
        let self = this
        // 看支持不支持FileReader
        if (!file.file || !window.FileReader) return
        if (/^image/.test(file.file.type)) {
            // 创建一个reader
            let reader = new FileReader()
            // 将图片2将转成 base64 格式
            reader.readAsDataURL(file.file)
            // 读取成功后的回调
            reader.onloadend = function() {
                let result = this.result
                let img = new Image()
                img.src = result
                //判断图片是否大于5M,是就直接上传，反之压缩图片
                if (this.result.length <= 1.5 * 1024 * 1024) {
                    console.log('图片小于1.5M')
                    // 上传图片
                    // self.postImg(this.result,file,num);
                    self.postImg(this.result,file);
                } else {
                    console.log('图片大于1.5M，需要压缩')
                    img.onload = function() {
                    let data = self.compress(img)
                    // 上传图片
                    // self.postImg(data,file,num);
                    self.postImg(data,file);
                    }
                }
            }
        }
    }
    // 压缩图片			
    compress(img) {
        let canvas = document.createElement('canvas')
        let ctx = canvas.getContext('2d')
        //瓦片canvas
        let tCanvas = document.createElement('canvas')
        let tctx = tCanvas.getContext('2d')
        // let initSize = img.src.length;
        let width = img.width
        let height = img.height
        //如果图片大于四百万像素，计算压缩比并将大小压至400万以下
        let ratio
        if ((ratio = (width * height) / 4000000) > 1) {
            // console.log("大于400万像素");
            ratio = Math.sqrt(ratio)
            width /= ratio
            height /= ratio
        } else {
            ratio = 1
        }
        canvas.width = width
        canvas.height = height
        //    铺底色
        ctx.fillStyle = '#fff'
        ctx.fillRect(0, 0, canvas.width, canvas.height)
        //如果图片像素大于100万则使用瓦片绘制
        let count
        if ((count = (width * height) / 1000000) > 1) {
            // console.log("超过100W像素");
            count = ~~(Math.sqrt(count) + 1) //计算要分成多少块瓦片
            //      计算每块瓦片的宽和高
            let nw = ~~(width / count)
            let nh = ~~(height / count)
            tCanvas.width = nw
            tCanvas.height = nh
            for (let i = 0; i < count; i++) {
                for (let j = 0; j < count; j++) {
                    tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh)
                    ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh)
                }
            }
        } else {
            ctx.drawImage(img, 0, 0, width, height)
        }
        //进行压缩
        let ndata = canvas.toDataURL('image/jpeg', 0.8)
        tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
        console.log(ndata)
        return ndata
    }
    
    //将base64转换为文件
    dataURLtoFile(dataurl,file) {
        var arr = dataurl.split(','),
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n)
        while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
        }
        return new File([u8arr], file.file.name, {
            type: file.file.type
        })
    }
                
    // 提交图片到后端
    postImg(base64,file,num) {
        let filedata = this.dataURLtoFile(base64,file)
        let formData = new window.FormData()
        formData.append('file', filedata)
        // 调用传的方法
        // this.fun(formData,file,num)
        this.setMyf(formData)
        this.setMyfb(base64)
        this.fun(...this.args, base64)
    }

    getMyf() {
        return this.myf
    }
    setMyf(myf) {
        this.myf = myf
    }
    getMyfb() {
        return this.myfb
    }
    setMyfb(myfb) {
        this.myfb = myfb
    }
                   
}